/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercises.day01.pt2;

import java.util.Random;
import java.util.Scanner;

/**
 * MyFirstJava
 *
 * @author (jaice)
 * @version 0.1 (may 28, 2012)
 * 
 * d. Guess the number: Write an application that plays "guess the number" as follows:
 * Your program chooses the number to be guessed by selecting a random integer in the range
 * 1 to 1000. The application displays the prompt Guess a number between 1 and 1000. The player
 * inputs a first guess. If the player's guess is incorrect, your program should display Too high.
 * Try again. or Too low. Try again. to help the player "zero in" on the correct answer. The program 
 * should prompt the user for the next guess. When the user enters the correct answer, display 
 * Congratulations. You guessed the number!, and allow the user to choose whether to play again.
 *
 * 
 */
public class GuessNumber {

    public static void main(String args[]) {
        //declare variables
        int guess = -1;
        int randomInt = 0;
        int playAgain = 0;

        do {

            //generate random integer
            Random randomGenerator = new Random();
            for (int idx = 1; idx <= 10; ++idx) {
                randomInt = randomGenerator.nextInt(1000);
            }
            //declare scanner
            Scanner sc = new Scanner(System.in);

            //prints guess number
            System.out.println("Guess number");


            do {
                //gets guess
                guess = sc.nextInt();
                //checks if guess is higher or lower
                if (guess < randomInt) {
                    System.out.println("Too low");
                } else {
                    System.out.println("Too high");
                }
                //wrong guess
                System.out.println("Guess again");

            } while (randomInt != guess); //if guesses right, get out of loop

            //asks if wants to play again
            System.out.println("Congratulations. You guessed the number!");
            System.out.println("Play again? Enter 9 to quit or any other number to continue");
            //gets if play again
            playAgain = sc.nextInt();

            //if 9, quit
        } while (playAgain != 9);
    }
}